package com.webserver.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * 该线程负责与指定客户端完成HTTP协议的交互过程
 *
 */
public class ClientHandler implements Runnable{
    private Socket socket;
    private String line;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }

    public void run() {
        while (true) {
            preCur();
            if ("".equals(line)){
                break;
            }
        }

    }
    public void preCur(){
        //测试:读取客户端发送过来的内容
        try {
            InputStream in = socket.getInputStream();
            int d;
            char pre = 'a';//记录上次读取到的字符
            char cur = 'a';//记录本次读取到的字符
            StringBuilder builder = new StringBuilder();

            while ((d = in.read())!=-1){
                cur  = (char) d;//本次读取到的字符
                if (pre==13&&cur==10){//是否连续读取到了回车+换行
                    break;
                }
                builder.append(cur);
                pre = cur;
            }
            line = builder.toString().trim();
            System.out.println("请求行内容:"+line);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
